//
//  CommodityWarehouseView.m
//  BusinessApp
//
//  Created by CccDaxIN on 2016/10/28.
//  Copyright © 2016年 macox. All rights reserved.
//

#import "CommodityWarehouseView.h"
#import "CommodityWarehouseTableViewCell.h"
#import "ReleaseCommoditySubModel.h"
#import "ReleaseCommodityViewController.h"

@implementation CommodityWarehouseView

- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
  
        [self createCommodityWarehouseViewTableView];
        [self commodityLibraryInformationDataRequest];
    }
    return self;
}

#pragma mark --  创建tableview
- (void)createCommodityWarehouseViewTableView{
    if ([[NNSingleton sharedSingleton].allAvailable isEqualToString:@"无"]) {
        _CommodityWView =[[UITableView alloc]initWithFrame:CGRectMake(0,0,kScrernWidth-90,kScrernHeight-64) style:UITableViewStylePlain];
    }else{
        _CommodityWView =[[UITableView alloc]initWithFrame:CGRectMake(0,0,kScrernWidth-90,kScrernHeight-114) style:UITableViewStylePlain];
    }
    _CommodityWView.showsVerticalScrollIndicator = FALSE;
    _CommodityWView.delegate=self;
    _CommodityWView.dataSource=self;
    _CommodityWView.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.94 alpha:1.00];
    _CommodityWView.tableFooterView = [[UIView alloc] init];
    [self addSubview:_CommodityWView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_data_array count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifer=@"commodityCell";
   CommodityWarehouseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CommodityWarehouseTableViewCell" owner:nil options:nil] firstObject];
    }
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_data_array.count != 0) {
        ReleaseCommoditySubModel *model = [_data_array objectAtIndex:indexPath.row];
        cell.model = model;
    }
    return cell;
}
#pragma mark -- 代理
- (void)choiceButtonActionDelegate:(NSString*)indexPath
{
    [NNSingleton sharedSingleton].commodityLibraryID = [NSString stringWithFormat:@"%@",indexPath];
    [NNSingleton sharedSingleton].releasePosition = @"商品库";
    ReleaseCommodityViewController *releaseCommodity = [[ReleaseCommodityViewController alloc]init];
    [self.navigationController pushViewController:releaseCommodity animated:YES];
}
#pragma mark -- 获取商品库信息
- (void)commodityLibraryInformationDataRequest{

    [SVProgressHUD showWithStatus:@"正在加载"];
    [GDJHttpTool get:CommodityLibraryInformation params:@{@"ShouJiHao":GETPHONE,@"Duuid":GETUUID,@"Token":GETTOKEN,@"CategoryId":[NNSingleton sharedSingleton].ERJIID,@"ProductName":@""} success:^(id responseObj) {
        NSLog(@"对面的女孩 %@",responseObj);
        //查看错误码
        NSInteger err = [[responseObj valueForKey:@"Err"]integerValue];
        NSLog(@"争取吧%ld",(long) err);
        if (err == 0) {

            _data_array =   [ReleaseCommoditySubModel mj_objectArrayWithKeyValuesArray:[responseObj objectForKey:@"Products"]];
        }
        if(_data_array.count == 0){
            [SVProgressHUD dismiss];
            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(self.center.x - 145,50, 120, 100)];
            image.image = [UIImage imageNamed:@"小哭脸"];
            [self addSubview:image];
            
            UILabel *imageLabel = [[UILabel alloc]init];
            imageLabel.frame = CGRectMake(0 , 170, kScrernWidth-90, 30);
            imageLabel.textAlignment = NSTextAlignmentCenter;
            imageLabel.textColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.33 alpha:1.00];
            imageLabel.font = [UIFont systemFontOfSize:13];
            imageLabel.text = @"商品库中没有您要找的商品";
            [self addSubview:imageLabel];

        }else{
            [SVProgressHUD dismiss];
        }
        [_CommodityWView reloadData];
    }failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"@@@@@@###   %ld",(long)indexPath.row);
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
